package com.blue.base.shiro.realms;

import com.blue.base.shiro.api.ShiroAuthService;
import com.blue.base.shiro.bean.Permission;
import com.blue.base.shiro.bean.Role;
import com.blue.base.shiro.bean.User;
import com.blue.base.shiro.util.ShiroUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * 用于进行权限信息的验证
 *
 * @author liulei
 * @version 1.0
 */
@Slf4j
@AllArgsConstructor
public class AuthRealms extends AuthorizingRealm implements InitializingBean {

    private ShiroUtil shiroUtil;

    private ShiroAuthService shiroAuthService;

    /**
     * 在需要判断权限时，会调用此方法获取用户的角色、权限信息
     *
     * @param principalCollection 主题规则
     * @return 授权信息
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        List<Role> roles = shiroUtil.getLoginAccount();
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        for (Role role : roles) {
            //添加角色
            simpleAuthorizationInfo.addRole(role.getName());
            //添加权限
            for (Permission permissions : role.getPermissions()) {
                if (!StringUtils.isEmpty(permissions.getUrl())) {
                    simpleAuthorizationInfo.addStringPermission(permissions.getUrl());
                }
            }
        }
        return simpleAuthorizationInfo;
    }

    /**
     * 登录认证获取用户，并且比对用户信息
     *
     * @param authenticationToken token
     * @return 认证信息，
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        if (Objects.isNull(authenticationToken.getPrincipal())) {
            return null;
        }
        //获取用户信息
        String account = authenticationToken.getPrincipal().toString();
        User emp = shiroAuthService.getByAccount(account);
        if (Objects.isNull(emp)) {
            log.info("shiro 验证异常，用户名不存在于数据库2!");
            throw new IllegalArgumentException("用户名或密码错误2");
        }
        return new SimpleAuthenticationInfo(account, emp.getPassword(), this.getName());
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        this.setCachingEnabled(true);
        this.setAuthenticationCachingEnabled(true);
        // 简化redisCache中各种缓存key的格式，摒弃掉默认的key前缀设定【全类名】
        this.setName("realms");
        this.setAuthorizationCacheName("authorization");
        this.setAuthenticationCacheName("authentication");
    }
}
